
$(document).ready(function(){
    init();
    //申请加入家族
    // $('.if-add .btn-default').on('click',familyAdd);
    // $('.if-add .btn-primary').on('click',function (){
    //     alert('你已经加入该家族了！')
    // });
    $('.if-add .add_family').on('click',function(){
        if($(this).hasClass("btn-primary")){
            alert('你已经加入该家族了！');
        }else{
            familyAdd($(this));
        }
    });
});

//添加家族
function init(){
    $('#familyAdd').on('click',function(){
        var userId = localStorage.getItem("userId");
        if(userId == "" || userId == null){
            localStorage.setItem('headurl', "/h5/index/home");
            window.location.href="/h5/user/login";
        }
        var mobile=localStorage.getItem("mobile");
        if(mobile==""||mobile==null){
            window.location.href="/h5/user/login";
        }
        $("#mask").css({
            "opacity":"0.5",
            "background":"#000000",
            "display":"block"
        })
        $('.family-add').css('display','block');
        $('#addCancel').click(function(){
            $('.family-add').css('display','none');
            $("#mask").css({
                "opacity":"1",
                "background":"#F9F9F9",
                "display":"none"
            })
        });
        $('#addSure').on('click',function(){
            var $fName = $('#fName').val();
            var $fDesp = $('#fDesp').val();
            if($fName == ''||$fDesp == ''){
                alert('家族名称、描述不能为空！');
                return false;
            }else{
                $.ajax({
                    type: "post",
                    url: '/api/v1/family/create',
                    data: {
                        fName:$fName,
                        fDesp:$fDesp,
                        userId:userId,
                        /*Id:$Id*/
                    },
                    success: function (data) {
                       // alert('家族添加成功！');
                       // alert(data);
                       console.log(data.result);
                       /* $(".create").append("<li><div class='container'><div class='row'>"
                            +"<div class='col-xs-4 family-image'><img src='../images/add-head.png' alt=''/>"
                            +"</div><div class='col-xs-4 family-detail'><p class='create_name'>"+$fName+"</p>"
                            +"<p class='create_desc'>"+$fDesp+"</p></div></div></div></li>")*/
                       /* $('.family-add').hide();
                        $('#fName').val("")
                        $('#fDesp').val("")*/
                        window.location.reload();
                    },
                    error: function (msg) {
                        alert('请求失败');
                        console.log(msg)
                    }
                });
        }
        })
    })
}

function familyAdd(obj) {
    $("#sendTest").val("")
    $(".testMess").show()
    $(".testMask").show()
    var userId=localStorage.getItem("userId");
    if(userId == "" || userId == null){
        localStorage.setItem('headurl',"/h5/index/home");
        window.location.href="/h5/user/login";
    }
/*    var mobile=localStorage.getItem("moblie");
    if(mobile=="" || mobile==null){
        localStorage.setItem('headurl',"/h5/index/home");
        window.location.href="/h5/user/login";
    }*/
    var f_id = obj.parent(".if-add").siblings(".fId").val();
    $(".sendBtn").on("click",function(){
        var fp_id = $("#sendTest").val();
  //      console.log(fp_id);
        $.ajax({
            type: "post",
            url: '/h5/family/createActives',
            data: {
                "fId":f_id,
                "userId":userId,
                "content":fp_id
            },
            success: function (data) {
                obj.addClass("btn-primary").html("已申请");//加入家族成功后样式
                $(".testMess").hide()
                $(".testMask").hide()
                //alert('已申请！');
                console.log(data.result);
            },
            error: function (msg) {
                alert('请求失败');
                console.log(msg)
            }
        });
    })

}
$(".cancelBtn").click(function(){
    $(".testMess").hide()
    $(".testMask").hide()
})

//跳转家族树
$(".family-detail").on("click",function () {
    // var userId=
    var fId=$(this).siblings(".fId").val();
    var personId=localStorage.getItem("personId");
    window.location.href="/h5/family/showtree?fId="+fId+"&personId="+personId;
})